package com.ssi.action.goods;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.processors.JsDateJsonBeanProcessor;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.ssi.action.CommonAction;
import com.ssi.domain.goods.Goods;
import com.ssi.domain.goodstype.Goodstype;
import com.ssi.domain.user.User;
import com.ssi.service.goods.GoodsService;
import com.ssi.service.goodstype.GoodstypeService;
import com.ssi.util.StringUtil;

/**
 * 
 * @author lxf
 *
 */
public class GoodsAction extends CommonAction {
	private GoodsService goodsService;
	private GoodstypeService goodstypeService;

	public GoodsService getGoodsService() {
		return goodsService;
	}

	public void setGoodsService(GoodsService goodsService) {
		this.goodsService = goodsService;
	}
	
	public GoodstypeService getGoodstypeService() {
		return goodstypeService;
	}

	public void setGoodstypeService(GoodstypeService goodstypeService) {
		this.goodstypeService = goodstypeService;
	}
	public String addGoodsPage() {
		return "addGoods";
		
	}
	public String addGoods() {
		Object obj =request.getParameter("data");
		
		JSONObject jsonObject=new JSONObject();
		if(obj==null || "".equals(data)){		
			jsonObject.put("msg", "error");
		}else{
			JSONArray goodsArray = JSONArray.fromObject(data);
			for (int i = 0; i < goodsArray.size(); i++) {
				JSONObject goodsJson=(JSONObject) goodsArray.get(i);
					Goods g=new Goods();
					g.setGoodsName(goodsJson.getString("goodsName"));
					g.setGoodsUrl(goodsJson.getString("goodsUrl"));
					g.setGoodsImgUrl(goodsJson.getString("goodsImgUrl"));
					g.setGoodsTypeId(goodsJson.getInt("goodsTypeId"));
					g.setSpreadTypeId(goodsJson.getInt("spreadTypeId"));
					g.setGoodsViewsCount(0);
					g.setUserId(goodsJson.getInt("userId"));
					g.setOther(goodsJson.getString("other"));					
					g.setCreateDate(StringUtil.getCurTime());
				 goodsService.addGoods(g);
			}	
			jsonObject.put("msg", "ok");
		}
		json=jsonObject.toString();
		return "json";
	}

	public String updateGoods(Goods goods) {
		goodsService.updateGoods(goods);
		return "";
	}
	public String deleteGoods(int goodsId) {
		goodsService.deleteGoods(goodsId);
		return "";
	}
						

	public String queryGoodsById() {
		String par = getParameters("id");
		int id = Integer.parseInt(par);
		goods=goodsService.queryGoodsById(id);
		setAttribute("goods", goods);
		if(goods==null){
			return "error";
		}else{
			return "goodsDetails";	
		}
		
	}
	public String queryGoodsByGoodstypeId(String goodstypeIds) {
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("ids", "1");
		map.put("top", 4);
		 goodsService.queryGoodsByGoodstypeId(map);
		 return "";
	}

	public String queryGoodsByName(String goodsName) {
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("goodsName", "");
		map.put("userId", 1);
		goodsService.queryGoodsByName(map);
		return "";
	}
					
	public String queryGoodsByGoodstypeId() {
		
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("ids", "1");
		map.put("top", 4);
		List<Goods> listGoods=goodsService.queryGoodsByGoodstypeId(map);
		return "";
	}
	public String entryQueryGoods(){
		User user = getUser();
		if(user!=null){
			return "queryGoods";
		}else{
			return "";
		}
	}
	public String queryGoods() {
		String goodsName = getParameters("goodsName");
		String spreadTypeId = getParameters("spreadTypeId");
		String goodsTypeId = getParameters("goodsTypeId");
		if("".equals(goodsName.trim()))goodsName=null;
		if("".equals(spreadTypeId.trim()))spreadTypeId=null;
		if("".equals(goodsTypeId.trim()))goodsTypeId=null;
		
		User user = getUser();
		if(user!=null){
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("userId", user.getId());
			map.put("goodsName", goodsName);
			map.put("spreadTypeId", spreadTypeId);
			map.put("goodsTypeId",goodsTypeId);
			listGoods=goodsService.queryGoods(map);
			JSONArray activityJson=JSONArray.fromObject(listGoods);			
			json=activityJson.toString();	
			return "json";
		}else{
			return "";
		}
		
		
	}
	public String queryMoreGoods(){
		String spreadType=getParameters("spreadType");
		String goodsType=getParameters("goodsType");
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("spreadType", spreadType);
		map.put("goodsType", goodsType);
		 listGoods=goodsService.queryMoreGoods(map);
		setAttribute("listGoods", listGoods);		
		return "queryMoreGoods";
		
	}
	String data;
	
	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}
	Goods goods;
	
	public Goods getGoods() {
		return goods;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
	}
	List<Goods> listGoods;
	public List<Goods> getListGoods() {
		return listGoods;
	}

	public void setListGoods(List<Goods> listGoods) {
		this.listGoods = listGoods;
	}
	String json;

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}
	

}
